<?php

namespace app\controllers;

use app\models\CardShow;
use yii\web\Controller;

class CardShowController extends Controller
{
    public $enableCsrfValidation = false;
    public $layout = 'manage_layout_js';

    public function actionMobileCardGoodsShow()
    {
        $type = \Yii::$app->request->get("type");
        $card_template_id = \Yii::$app->request->get('card_template_id');

        $tpl_info = CardShow::getIntegralCard_IMG($card_template_id);
        if (count($tpl_info) > 0) {
            $tpl_info = $tpl_info[0];
        }
//        $this->layout = false;
        return $this->render('mobile-card-goods-show', ['type' => $type, 'card_template_id' => $card_template_id, 'tpl_info' => $tpl_info]);
    }

    public function actionMobileCardGoodsShowPartial()
    {
        $type = \Yii::$app->request->get("type");
        $card_template_id = \Yii::$app->request->get('card_template_id');
        $page = \Yii::$app->request->get('page');

        $pageSize = 10;
//        $totalCount = CardShow::getIntegralCardTPLGoodsCount($card_template_id);

//        $pagination = new Pagination([
//            'defaultPageSize' => $pageSize,
//            'totalCount' => $totalCount,
//        ]);

        $items = CardShow::getIntegralCardTPLGoods($pageSize * $page, $pageSize, $card_template_id);
//        foreach ($items as &$item) {
//            $item['PIC'] = NCInterFace::getGiftPic($item['CODE']);
////            if (empty($item['DESCRIPTION'])) {
////                $item['DESCRIPTION'] = NCInterFace::getGiftContent($item['CODE']);
////            }
//        }
        return $this->renderPartial('mobile-card-goods-show-partial', ['items' => $items]);
    }

    public function actionMobileCardGoodsShowDetailHandler()
    {
        $code = \Yii::$app->request->post("code");
        $detail = CardShow::getGoodsDetail($code);
        return $this->renderJson($detail['detail_html']);
    }

    /**
     * @param array|mixed $data
     * @param int $status 0:success
     * @param string $message
     * @return string
     */
    public function renderJson($data = null, $status = 0, $message = 'success')
    {
        $result = [
            'status' => $status,
            'message' => $message,
            'data' => $data
        ];

        return json_encode($result, true);
    }
}